package com.youfan.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.youfan.entity.ProductType;
import com.youfan.mapper.ProducttypeMapper;
import com.youfan.page.CommonPage;
import com.youfan.service.ProductTypeService;
import com.youfan.service.SearchService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class ProducttypeServiceImpl extends ServiceImpl<ProducttypeMapper, ProductType> implements ProductTypeService {

    @Autowired
    private ProducttypeMapper producttypeMapper;
    @Autowired
    private SearchService searchService;

    @Override
    public int insertOne(ProductType youfanProductType) {
        youfanProductType.setCreateTime(new Date());
        int result=producttypeMapper.insert(youfanProductType);
        JSONObject json=JSONObject.parseObject(JSONObject.toJSONString(youfanProductType));
        json.put("index","shopyun");
        json.put("type","shopyun");
        json.put("id",youfanProductType.getId()+"");
        searchService.addData(json);
        return result;
    }
    @Override
    public CommonPage<ProductType> queryByCondtion(ProductType youfanProductType) {
        PageHelper.startPage(youfanProductType.getPageNum(),youfanProductType.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(youfanProductType.getName())){
            queryWrapper.like("name", youfanProductType.getName());
        }
        if(StringUtils.isNotBlank(youfanProductType.getProductypeDesc())){
            queryWrapper.like("productype_desc", youfanProductType.getProductypeDesc());
        }

            queryWrapper.eq("parentid", youfanProductType.getParentid());

        List<ProductType> list = producttypeMapper.selectList(queryWrapper);


        return CommonPage.restPage(list);
    }

    @Override
    public ProductType findByid(ProductType youfanProductType) {
        return producttypeMapper.selectById(youfanProductType.getId());
    }

    @Override
    public int updateOne(ProductType youfanProductType) {
        youfanProductType.setUpdateTime(new Date());
        int result=producttypeMapper.updateById(youfanProductType);
        JSONObject json=JSONObject.parseObject(JSONObject.toJSONString(youfanProductType));
        json.put("index","shopyun");
        json.put("type","shopyun");
        json.put("id",youfanProductType.getId()+"");
        searchService.updateDataById(json);
        return result;
    }
}
